package pri.jz.keeper.ui.register

import androidx.activity.viewModels
import androidx.compose.runtime.Composable
import pri.jz.keeper.ui.base.BaseActivity
import pri.jz.keeper.ui.uistate.RegisterState
import pri.jz.keeper.ui.view.RegisterView
import pri.jz.keeper.ui.base.BaseViewModel

class RegisterActivity : BaseActivity() {
    private val viewModel: RegisterViewModel by viewModels()
    @Composable
    override fun OnCreateView() {
        RegisterView()
    }

    override suspend fun onObserver() {
        viewModel.uiState.collect {
            when(it.state) {
                RegisterState.DEFAULT -> {}
                RegisterState.SUCCESS -> {registerSuccess()}
                RegisterState.CANCEL -> {registerCancel()}
            }
        }

    }

    private fun registerCancel() {
        finish()
    }

    private fun registerSuccess() {
        finish()
    }

    override fun getViewModel(): BaseViewModel {
        return viewModel
    }

//    private fun onRegister(number: MutableState<String>, password: MutableState<String>, code: MutableState<String>) {
//        if (number.value.length != 11) {
//            showTip("手机号格式不正确")
//        } else if (password.value.length !in 6..20) {
//            showTip("密码格式不正确")
//        } else if (!codeCheck(code.value)){
//            showTip("验证码不正确")
//        } else {
//            registerSuccess(number.value,password.value)
//        }
//    }

    /**
     * 注册成功
     * @param number
     * @param password
     */
//    private fun registerSuccess(number: String,password: String) {
//        requestLoadLauch {
//            val userDao = HttpFactory.retrofitClient.create(UserRDao::class.java)
//            val g = GsonBuilder()
//                .setDateFormat("yyyy-MM-dd HH:mm:ss").create()
//
//            val respose = userDao.userRegister(UserT(number = number, password = password).apply {
//                Log.d(TAG, "registerSuccess: ${g.toJson(this)}")
//            }).execute()
//            if (respose.code() == 200 && respose.body()!! > 0) {
//                showTip("注册成功") {
//                    finish()
//                }
//            } else {
//                showTip("注册失败，请重试")
//            }
//        }
//    }

    /**
     * TODO 验证码对比
     *
     * @param code
     * @return
     */
    private fun codeCheck(code: String): Boolean {

        return true
    }

    /**
     * 发送验证码
     */
    private fun sendCode() {
//        showTip("验证码功能暂不可用，默认正确")
    }
}